import java.util.Arrays;

public class Solution {
    public static int minInsertions(String s) {
        int len = s.length();
        char[] ch = s.toCharArray();

        int[][] dp = new int[len][len];

        int ret = 0;
        for (int i = len - 1; i >= 0; i--) {//从下往上
            for (int j = i; j < len; j++) {//从左往右
                if (ch[i] == ch[j]) {
                    if (i == j) {
                        dp[i][j] = 1;
                    } else if (i + 1 == j) {
                        dp[i][j] = 2;
                    } else {
                        dp[i][j] = dp[i + 1][j - 1] + 2;
                    }
                } else {
                    dp[i][j] = Math.max(dp[i][j - 1], dp[i + 1][j]);
                }
                ret = Math.max(ret, dp[i][j]);
            }
        }

        return len - ret;
    }

    public static void main(String[] args) {
        System.out.println(minInsertions("mbadm"));
    }
}
